/* Name: PriorityQueue
 * Author: Cameron Hoerig
 * Date: 5-1-12
 * Description: The PriorityQueue class manages the operation of a priority queue. The queue
 * 	is stored as a linked list, as it most efficient for pop operations. Pushing, which is n complexity
 *  only occurs during the creation of a room and only a few times during gameplay.
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.util.*;

public class PriorityQueue {
	
	public PriorityQueue(){};
	
	private LinkedList<Event> priorityQueue = new LinkedList<Event>();
	
	// Add a new event to the priority queue, keeping the order of the priorities
	public void push(Event newEvent){
		Boolean inserted = false;
	    for(ListIterator it = priorityQueue.listIterator(); it.hasNext();) {
	        int index = it.nextIndex();
	    	Event eventOn = (Event)it.next();
	        if (newEvent.getPriority() <= eventOn.getPriority()) {
	        	priorityQueue.add(index, newEvent);
	        	inserted = true;
	        	break;
	        }
	    }
	    if (!inserted) {
	    	priorityQueue.addLast(newEvent);
	    }
	}
	
	// Adds an event to the end of the list
	public void addToLast(Event newEvent) {
		priorityQueue.addLast(newEvent);
	}
	
	// Adds an event to the front of the list
	public void addToFirst(Event newEvent) {
		priorityQueue.addFirst(newEvent);
	}
	
	// Removes an event with a certain item from the priority queue
	public Boolean removeItem(Item it) {
		for (int i = 0; i < getSize(); i++) {
			if (it == priorityQueue.get(i).getItem()) {
				priorityQueue.remove(i);
				return true;
			}
		}
		return false;
	}
	
	// Returns and removes the first event from the priority queue
	public Event pop(){
		return priorityQueue.pop();
	}
	
	// Returns and removes the last event from the priority queue
	public Event popBack(){
		return priorityQueue.removeLast();
	}
	
	// Returns the size of the priority queue
	public int getSize(){
		// return the size of the queue
		return priorityQueue.size();
	}
	
	// Returns whether or not the priority queue is empty
	public Boolean isEmpty() {
		return (getSize() == 0); 
	}

}
